package com.dc.refactoring._3_重新组织数据._14_ReplaceSubClassWithFields.step2;

/**
 * Description: Person 人 抽象类
 * @author: gdc
 * @date: 2020/12/4
 * @version 1.0
 */
public abstract class Person {

    // 【3】针对每个常量函数，在超类中声明一个对应的字段
    private final boolean isMale;
    private final char code;

    // 【4】父类中新建受保护的构造函数
    protected Person(boolean isMale, char code) {
        this.isMale = isMale;
        this.code = code;
    }

    abstract boolean isMale();

    abstract char getCode();

    // 【1】在父类中为所有子类创建工厂函数
    static Person createMale(){
        return new Male();
    }

    static Person createFemale(){
        return new Female();
    }

}


class Male extends Person {

    // 【5】为子类添加新的构造函数
    public Male() {
        super(true, 'M');
    }

    @Override
    boolean isMale() {
        return true;
    }

    @Override
    char getCode() {
        return 'M';
    }
}

class Female extends Person {

    // 【5】为子类添加新的构造函数
    public Female() {
        super(false, 'F');
    }

    @Override
    boolean isMale() {
        return false;
    }

    @Override
    char getCode() {
        return 'F';
    }
}